home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / pbmplus / pbm / pbmtolj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.5 KB  |  170 lines

  1. /* pbmtolj.c - read a portable bitmap and produce a LaserJet bitmap file
  2. **    
  3. **    based on pbmtops.c
  4. **
  5. **    Michael Haberler HP Vienna mah@hpuviea.uucp
  6. **                   mcvax!tuvie!mah
  7. **    misfeatures: 
  8. **        no positioning
  9. **
  10. **      Bug fix Dec 12, 1988 :
  11. **              lines in putbit() reshuffled 
  12. **              now runs OK on HP-UX 6.0 with X10R4 and HP Laserjet II
  13. **      Bo Thide', Swedish Institute of Space Physics, Uppsala <bt@irfu.se>
  14. **
  15. ** Copyright (C) 1988 by Jef Poskanzer and Michael Haberler.
  16. **
  17. ** Permission to use, copy, modify, and distribute this software and its
  18. ** documentation for any purpose and without fee is hereby granted, provided
  19. ** that the above copyright notice appear in all copies and that both that
  20. ** copyright notice and this permission notice appear in supporting
  21. ** documentation.  This software is provided "as is" without express or
  22. ** implied warranty.
  23. */
  24.  
  25. #include "pbm.h"
  26.  
  27. static int dpi = 75;
  28.  
  29. static void putinit ARGS(( void ));
  30. static void putbit ARGS(( bit b ));
  31. static void putrest ARGS(( void ));
  32. static void putitem ARGS(( void ));
  33.  
  34. void
  35. main( argc, argv )
  36.     int argc;
  37.     char* argv[];
  38.     {
  39.     FILE* ifp;
  40.     bit* bitrow;
  41.     register bit* bP;
  42.     int argn, rows, cols, format, rucols, padright, row;
  43.     register int nzcol, col;
  44.     char* usage = "[-resolution N] [pbmfile]\n\tresolution = [75|100|150|300] (dpi)";
  45.  
  46.     pbm_init( &argc, argv );
  47.  
  48.     argn = 1;
  49.  
  50.     /* Check for flags. */
  51.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  52.     {
  53.     if ( pm_keymatch( argv[argn], "-resolution", 2 ) )
  54.         {
  55.         ++argn;
  56.         if ( argn == argc || sscanf( argv[argn], "%d", &dpi ) != 1 )
  57.         pm_usage( usage );
  58.         }
  59.     else
  60.         pm_usage( usage );
  61.     ++argn;
  62.     }
  63.  
  64.     if ( argn != argc )
  65.     {
  66.     ifp = pm_openr( argv[argn] );
  67.     ++argn;
  68.     }
  69.     else
  70.     ifp = stdin;
  71.  
  72.     if ( argn != argc )
  73.     pm_usage( usage );
  74.  
  75.     pbm_readpbminit( ifp, &cols, &rows, &format );
  76.     bitrow = pbm_allocrow( cols );
  77.  
  78.     putinit( );
  79.     for ( row = 0; row < rows; ++row )
  80.     {
  81.     pbm_readpbmrow( ifp, bitrow, cols, format );
  82.  
  83.     /* Find rightmost black pixel. */
  84.     for ( nzcol = cols - 1; nzcol >= 0 && bitrow[nzcol] == PBM_WHITE; --nzcol )
  85.         continue;
  86.  
  87.     /* Round up to the nearest multiple of 8. */
  88.     rucols = ( nzcol + 8 ) / 8;
  89.     rucols = rucols * 8;
  90.     padright = rucols - (nzcol + 1);
  91.  
  92.     /* Transfer raster graphics */
  93.      printf("\033*b%dW",rucols/8);
  94.         for ( col = 0, bP = bitrow; col <= nzcol; ++col, ++bP )
  95.         putbit( *bP );
  96.     for ( col = 0; col < padright; ++col )
  97.         putbit( 0 );
  98.         }
  99.  
  100.     pm_close( ifp );
  101.  
  102.     putrest( );
  103.  
  104.     exit( 0 );
  105.     }
  106.  
  107. static int item, bitsperitem, bitshift, itemsperline, firstitem;
  108.  
  109. static void
  110. putinit( )
  111.     {
  112.     /* Printer reset. */
  113.     printf("\033E");
  114.  
  115.     /* Ensure top margin is zero */
  116.     printf("\033&l0E");
  117.  
  118.     /* Set raster graphics resolution */
  119.     printf("\033*t%dR",dpi);
  120.  
  121.     /* Start raster graphics, relative adressing */
  122.     printf("\033*r1A");
  123.  
  124.     itemsperline = 0;
  125.     bitsperitem = 1;
  126.     item = 0;
  127.     bitshift = 7;
  128.     firstitem = 1;
  129.     }
  130.  
  131. #if __STDC__
  132. static void
  133. putbit( bit b )
  134. #else /*__STDC__*/
  135. static void
  136. putbit( b )
  137. bit b;
  138. #endif /*__STDC__*/
  139.     {
  140.     if ( b == PBM_BLACK )
  141.     item += 1 << bitshift;
  142.     bitshift--;
  143.     if ( bitsperitem == 8 ) {
  144.     putitem( );
  145.         bitshift = 7;
  146.     }
  147.     bitsperitem++;
  148.     }
  149.  
  150. static void
  151. putrest( )
  152.     {
  153.     if ( bitsperitem > 1 )
  154.     putitem( );
  155.  
  156.     /* end raster graphics */
  157.     printf( "\033*rB" );
  158.  
  159.     /* Printer reset. */
  160.     printf("\033E");
  161.     }
  162.  
  163. static void
  164. putitem( )
  165.     {
  166.     putchar( item );
  167.     bitsperitem = 0;
  168.     item = 0;
  169.     }
  170.